package ui.tests;

import org.openqa.selenium.WebElement;
import org.testng.Assert;
import pages.ComparisonPage;
import pages.HomePage;
import utils.Log4Test;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: admin
 * Date: 29.05.14
 * Time: 20:06
 * To change this template use File | Settings | File Templates.
 */
public class CommonTestSteps extends BasicTest{
    private static final int PROD1_NUMBER = 2;
    private static final int PROD2_NUMBER = 3;

    public void login()
    {
        HomePage page = new HomePage(driver);
        page.open();
        Assert.assertTrue(page.isOpened(), Log4Test.error("Could not open page"));
    }

    public void searchProduct(String productName)
    {
        HomePage page =new HomePage(driver);
        page.searchProduct(productName);
        Log4Test.info("found product" + productName);
    }

    public void addToComparison()
    {
        HomePage page =new HomePage(driver);
        page.addProductToComparison();
        Log4Test.info("Added to comparison");
    }

    public void comparisonWorks(String prod1, String prod2)
    {
        HomePage page = new HomePage(driver);
        page.compareProducts();
        Assert.assertTrue(page.productPresent(prod1));
        Assert.assertTrue(page.productPresent(prod2));
    }

    public String[] getOlnyDifference(String prod1, String prod2) {
        ComparisonPage comparisonPage = new ComparisonPage(driver);
        List<WebElement> keys = comparisonPage.getKeyValue();
        List<WebElement> prod1Values = comparisonPage.getProdValue(PROD1_NUMBER);
        List<WebElement> prod2Values = comparisonPage.getProdValue(PROD2_NUMBER);
        String[] res = new String[keys.size()];
        int j = 0;
        for (int i = 0; i < keys.size(); i++) {
            if (!prod1Values.get(i).getText().equals(prod2Values.get(i).getText())) {
                res[j] = keys.get(i).getText();
                j++;
            }
        }
        return res;
    }
}
